package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee,HttpSession session){

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lqw);//where username = ???
        if (one == null) {
            return R.error("用户名不存在");
        }
        String rawPwd = employee.getPassword(); //用户传过来的密码
        String dbPwd = one.getPassword();   //数据库的密码
        String pwd = DigestUtils.md5DigestAsHex(rawPwd.getBytes()); //明文转为加密
        if (!dbPwd.equals(pwd)) {
            return R.error("密码错误");
        }
        Integer status = one.getStatus();
        if(status == 0){
            return R.error("员工已禁用");
        }
        session.setAttribute("employee",one.getId());
        one.setPassword(null);
        return  R.success(one);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        //清理session中的员工数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee){
        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return  R.success("新增员工成功");
    }

    /**
     * 员工分页查询
     * @param pageSize
     * @param page
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int pageSize,int page,String name){
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName, name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    /**
     * 员工信息修改
     * @param employee
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee){
        //从sesion中获取员工id，因为我们将sesion中id赋给线程了，所以直从线程中获取
        long id = Thread.currentThread().getId();
        //调用方法
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
